// page/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseURL: wx.baseURL,
    indicatorDots:true,
    vertical:false,
    autoplay:true,
    circular:true,
    interval:2000,
    duration:500,
    banners: [],
    news: [],
    Icon: [],
    recommending:[],
    newsIndex: 0,
    newsTimer: 0,
    newsItems:'transform: translate(0rpx, 0rpx);transition: transform 0.3s',
    pageNum: 1,
    lists: [],
    rows: 0,
    total: 0,
    timer: 0,
    loading: '数据加载中...'
  },
  click: function (e) {
    console.log(e)
    var name = e.currentTarget.dataset.name;
    console.log('点击姓名' + name)
  },

  //下拉刷新
  onPullDownRefresh(){
    this.data.pageNum = 1
    this.data.lists = []
    Promise.all([this.getBanners(),this.getNews(),this.getIcon(),this.getRecomend()]).then(() => {
      wx.stopPullDownRefresh()
    }).catch(e => {
      wx.stopPullDownRefresh()
    })
  },

  //轮播图
  getBanners() {
    return new Promise((resolve,reject) => {
      wx.get('http://localhost:3000/wx/banners').then((res) => {
        this.setData({
          banners: res
        })
      })
      resolve()
    })
  },
  //轮播图跳转事件
  SwiperClick(){
    let path = this.data.banners.path
    wx.switchTab({
      url: this.path,
    })
  },

  //新闻
  getNews() {
    return new Promise((resolve,reject) => {
      wx.get('http://localhost:3000/wx/news').then((res) => {
        this.setData({
          news:res
        })
        this.changeNewsIndex() //触发新闻轮播
        resolve()
      })
    })
  },

  //新闻轮播
  changeNewsIndex() {
    if (this.data.newsTimer) {
      clearInterval(this.data.newsTimer)
      this.data.newsTimer = 0
    }
    this.data.newsTimer = setInterval(() => {
      let newsIndex = this.data.newsIndex
      newsIndex++
      if (newsIndex == this.data.news.length) newsIndex = 0
      this.setData({
        newsIndex
      })
    }, 3000)
  },

  //获取分类
  getIcon() {
    return new Promise((resolve,reject) => {
      wx.get('http://localhost:3000/wx/categorys').then((res) => {
        this.setData({
            Icon:res
          })
          resolve()
        })   
      })
    },

  onReachBottom(){
    if(this.data.total > this.data.lists.length){
      this.data.pageNum ++
      this.getRecomend()
    }
  },
  getRecommend(){
    return new Promise((resolve,reject)=>{
      this.setData({loading:'数据加载中...'})
      wx.get('http://localhost:3000/wx/recommends' ,{
        pageNum:this.data.pageNum
      }).then((res) => {
        // console.log('打印推荐数据',res.data)
        this.setData({
          loading:'小主，已经到底了哦~',
          recommending:this.data.recommending.concat(res.rows),
          total:res.total
        })
        // reject(111)
        resolve()
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    Promise.all([this.getNews(),this.getBanners(),this.getIcon(),this.getRecommend()]).then(() => {
      console.log('所有数据加载完成')
    }).catch(e => {
      console.log(e)
    })
    this.changeNewsIndex()
    this.onReachBottom()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getNews(() => {
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
